﻿using ConsoleApplication1.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ConsoleApplication1.Util
{
    public class Relogio 
    {
        delegate void atualizaHoraCallback(string text);
        public delegate void ControlStringConsumer(Control control, string text);

        public void atualizaHora()
        {
            while (true)
            {
                atualizaHora(System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
            }
        }

        private void atualizaHora(string text)
        {
            // InvokeRequired required compares the thread ID of the 
            // calling thread to the thread ID of the creating thread. 
            // If these threads are different, it returns true. 
            if (Principal.lhora.InvokeRequired)
            {
                //atualizaHora(System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                atualizaHoraCallback d = new atualizaHoraCallback(atualizaHora);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                //Console.Write("oi");
                Principal.lhora.Text = System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
            }
        }

        private void Invoke(atualizaHoraCallback d, object[] p)
        {
            Principal.lhora.Text = System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
        }
    }
}
